package com.example.demo.controller;

import com.example.demo.constant.Constant;
import com.example.demo.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Boolean login(String name, String password,HttpSession session){
        log.info("用户登录：name: "+name+" password: "+password);
        //参数校验
        if(!StringUtils.hasLength(name)||!StringUtils.hasLength(password)){
            return false;
        }
        try{
           Boolean result= userService.login(name,password);
           log.info("用户登录成功{}",result);
           session.setAttribute(Constant.USER_NAME,name);
           return result;

        }catch (Exception e){
            log.error("登录返回异常：{}" ,e);
            return  false;
        }

    }
}
